import Vue from 'vue'
import VueRouter from 'vue-router'
import {getToken} from '@/utils/token.js'
// import login from '@/views/login/index.vue'
// import layout from '@/views/Layout/index.vue'
// import home from '@/views/home/index.vue'
// import user from '@/views/user/index.vue'
// import search from '@/views/search/index.vue'
// import searchresult from '@/views/search/searchresult/index.vue'
// import articledetail from '@/views/ArticleDetail/index.vue'
// import useredit from '@/views/user/UserEdit.vue'
// import chat from '@/views/chat/index.vue'
Vue.use(VueRouter)
// 总结:
// 路由懒加载: 为了让第一个页面, 加载的app.js小一点, 打开网页快一点
// 思路: 把组件对应js, 分成若干个.js, 路由切换到哪个页面, 才去加载对应的.js文件
// 原因: webpack分析入口时, 发现router里上来就import所有页面, 所以直接打包进app.js->很大
// 解决: 当路由路径匹配规则时, 才现去import引入对应的组件js文件
// webpackChunName注释语法, 给生成的js文件起名字
const routes = [
  {
    path:'/',
    redirect:'/layout'
  },
  {
    path:'/useredit',
    component:()=>import('@/views/user/UserEdit.vue')
  },
  {
    path:'/chat',
    component:()=>import('@/views/chat/index.vue')
  },
  {
    path:'/search',
    component:()=>import('@/views/search/index.vue')
  },
  {
    path:'/articledetail',
    component:()=>import('@/views/ArticleDetail/index.vue')
  },
  {
    path:'/searchresult',
    component:()=>import('@/views/search/searchresult/index.vue')
  },
  {path:'/login',
  component:()=>import('@/views/login/index.vue')
},
  {path:'/layout',
  component:()=>import('@/views/Layout/index.vue'),
  children:[
     {path:'home', component:()=>import('@/views/home/index.vue'),meta:{scrollt:0}},
     {path:'user', component:()=>import('@/views/user/index.vue')},
     {path:'/', redirect: '/layout/home'}
  ]}
]

const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{ //路由前置守卫
    if(getToken()?.length>0 && to.path =='/login'){//如果已经登录就不能让进入登录页
        //next(false)
        next('/')  //想要进入登录页就返回首页即可
    }else{
       next()
    }
})
export default router
